/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JFTelefones.java
 *
 * Created on 29/11/2012, 10:28:14
 */
package contatospro;

import java.awt.Component;
import java.awt.EventQueue;
import java.beans.Beans;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.RollbackException;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author Junnyor S2 Rafa
 */
public class JFTelefones extends JPanel {
    
    public JFTelefones() {
        initComponents();
        if (!Beans.isDesignTime()) {
            entityManager.getTransaction().begin();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("contatosproPU").createEntityManager();
        query = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT t FROM Telefones t");
        list = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(query.getResultList());
        pessoasQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT p FROM Pessoas p");
        pessoasList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : pessoasQuery.getResultList();
        pessoasQuery1 = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT p FROM Pessoas p");
        pessoasList1 = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : pessoasQuery1.getResultList();
        telefones1 = new contatospro.Telefones();
        pessoasQuery2 = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT p FROM Pessoas p");
        pessoasList2 = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : pessoasQuery2.getResultList();
        masterScrollPane = new javax.swing.JScrollPane();
        masterTable = new javax.swing.JTable();
        codPessoaLabel = new javax.swing.JLabel();
        telefoneLabel = new javax.swing.JLabel();
        tipoTelefoneLabel = new javax.swing.JLabel();
        jToolBar1 = new javax.swing.JToolBar();
        newButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        refreshButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        pessoajComboBox1 = new javax.swing.JComboBox();
        buscarjButton = new javax.swing.JButton();
        pessoajComboBox = new javax.swing.JComboBox();
        tipotelefonejComboBox = new javax.swing.JComboBox();
        telefonejFormattedTextField = new javax.swing.JFormattedTextField();

        FormListener formListener = new FormListener();

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.codPessoa}"), telefones1, org.jdesktop.beansbinding.BeanProperty.create("codPessoa"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.codTelefone}"), telefones1, org.jdesktop.beansbinding.BeanProperty.create("codTelefone"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.telefone}"), telefones1, org.jdesktop.beansbinding.BeanProperty.create("telefone"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.tipoTelefone}"), telefones1, org.jdesktop.beansbinding.BeanProperty.create("tipoTelefone"));
        bindingGroup.addBinding(binding);

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, list, masterTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${codTelefone}"));
        columnBinding.setColumnName("Cod Telefone");
        columnBinding.setColumnClass(Integer.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${codPessoa.pessoa}"));
        columnBinding.setColumnName("Pessoa");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${telefone}"));
        columnBinding.setColumnName("Telefone");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${tipoTelefone}"));
        columnBinding.setColumnName("Tipo Telefone");
        columnBinding.setColumnClass(String.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        masterScrollPane.setViewportView(masterTable);

        codPessoaLabel.setText("Pessoa:");

        telefoneLabel.setText("Telefone:");

        tipoTelefoneLabel.setText("Tipo de Telefone:");

        jToolBar1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jToolBar1.setFloatable(false);
        jToolBar1.setMaximumSize(new java.awt.Dimension(1000, 70));

        newButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/contatospro/Icones/Novo6.png"))); // NOI18N
        newButton.setText("Novo");
        newButton.setToolTipText("Novo Cadastro");
        newButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        newButton.setMaximumSize(new java.awt.Dimension(50, 60));
        newButton.setPreferredSize(new java.awt.Dimension(50, 60));
        newButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        newButton.addActionListener(formListener);
        jToolBar1.add(newButton);

        saveButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/contatospro/Icones/Salvar1.png"))); // NOI18N
        saveButton.setText("Salvar");
        saveButton.setToolTipText("Salvar Telefone");
        saveButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        saveButton.setMaximumSize(new java.awt.Dimension(50, 60));
        saveButton.setPreferredSize(new java.awt.Dimension(50, 60));
        saveButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        saveButton.addActionListener(formListener);
        jToolBar1.add(saveButton);

        refreshButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/contatospro/Icones/Atualizar1.png"))); // NOI18N
        refreshButton.setText("Atualizar");
        refreshButton.setToolTipText("Atualizar Tela");
        refreshButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        refreshButton.setMaximumSize(new java.awt.Dimension(65, 60));
        refreshButton.setPreferredSize(new java.awt.Dimension(65, 60));
        refreshButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        refreshButton.addActionListener(formListener);
        jToolBar1.add(refreshButton);

        deleteButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/contatospro/Icones/Excluir3.png"))); // NOI18N
        deleteButton.setText("Excluir");
        deleteButton.setToolTipText("Excluir Telefone");
        deleteButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        deleteButton.setMaximumSize(new java.awt.Dimension(50, 60));
        deleteButton.setPreferredSize(new java.awt.Dimension(50, 60));
        deleteButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), deleteButton, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        deleteButton.addActionListener(formListener);
        jToolBar1.add(deleteButton);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Pesquisar", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N
        jPanel1.setMinimumSize(new java.awt.Dimension(100, 50));

        pessoajComboBox1.setRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(
                JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if (value instanceof Pessoas) {
                    Pessoas mec = (Pessoas)value;
                    setText(mec.getPessoa());
                }
                return this;
            }
        }
    );

    org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, pessoasList2, pessoajComboBox1);
    bindingGroup.addBinding(jComboBoxBinding);
    binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, telefones1, org.jdesktop.beansbinding.ELProperty.create("${codPessoa}"), pessoajComboBox1, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
    bindingGroup.addBinding(binding);

    pessoajComboBox1.addKeyListener(formListener);

    buscarjButton.setText("Buscar");
    buscarjButton.addActionListener(formListener);

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGap(31, 31, 31)
            .addComponent(pessoajComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(buscarjButton)
            .addContainerGap(20, Short.MAX_VALUE))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(pessoajComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(buscarjButton))
            .addContainerGap(21, Short.MAX_VALUE))
    );

    pessoajComboBox.setRenderer(new DefaultListCellRenderer() {
        @Override
        public Component getListCellRendererComponent(
            JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (value instanceof Pessoas) {
                Pessoas mec = (Pessoas)value;
                setText(mec.getPessoa());
            }
            return this;
        }
    }
    );

    jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, pessoasList1, pessoajComboBox);
    bindingGroup.addBinding(jComboBoxBinding);
    binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, telefones1, org.jdesktop.beansbinding.ELProperty.create("${codPessoa}"), pessoajComboBox, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
    bindingGroup.addBinding(binding);

    tipotelefonejComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Residencial", "Comercial", "Celular", "Outros", " " }));

    binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, telefones1, org.jdesktop.beansbinding.ELProperty.create("${tipoTelefone}"), tipotelefonejComboBox, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
    bindingGroup.addBinding(binding);

    telefonejFormattedTextField.setBackground(new java.awt.Color(204, 255, 255));
    try {
        telefonejFormattedTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##)####-####")));
    } catch (java.text.ParseException ex) {
        ex.printStackTrace();
    }

    binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, telefones1, org.jdesktop.beansbinding.ELProperty.create("${telefone}"), telefonejFormattedTextField, org.jdesktop.beansbinding.BeanProperty.create("value"));
    bindingGroup.addBinding(binding);
    binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), telefonejFormattedTextField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
    bindingGroup.addBinding(binding);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(masterScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 411, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 411, Short.MAX_VALUE)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(telefoneLabel)
                        .addComponent(codPessoaLabel)
                        .addComponent(tipoTelefoneLabel))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(pessoajComboBox, 0, 323, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(telefonejFormattedTextField, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tipotelefonejComboBox, javax.swing.GroupLayout.Alignment.LEADING, 0, 137, Short.MAX_VALUE)))))
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(masterScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(codPessoaLabel)
                .addComponent(pessoajComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(telefoneLabel)
                .addComponent(telefonejFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(tipoTelefoneLabel)
                .addComponent(tipotelefonejComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    bindingGroup.bind();
    }

    // Code for dispatching events from components to event handlers.

    private class FormListener implements java.awt.event.ActionListener, java.awt.event.KeyListener {
        FormListener() {}
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            if (evt.getSource() == newButton) {
                JFTelefones.this.newButtonActionPerformed(evt);
            }
            else if (evt.getSource() == saveButton) {
                JFTelefones.this.saveButtonActionPerformed(evt);
            }
            else if (evt.getSource() == refreshButton) {
                JFTelefones.this.refreshButtonActionPerformed(evt);
            }
            else if (evt.getSource() == deleteButton) {
                JFTelefones.this.deleteButtonActionPerformed(evt);
            }
            else if (evt.getSource() == buscarjButton) {
                JFTelefones.this.buscarjButtonActionPerformed(evt);
            }
        }

        public void keyPressed(java.awt.event.KeyEvent evt) {
        }

        public void keyReleased(java.awt.event.KeyEvent evt) {
            if (evt.getSource() == pessoajComboBox1) {
                JFTelefones.this.pessoajComboBox1KeyReleased(evt);
            }
        }

        public void keyTyped(java.awt.event.KeyEvent evt) {
        }
    }// </editor-fold>//GEN-END:initComponents

    

    @SuppressWarnings("unchecked")
    private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refreshButtonActionPerformed
        entityManager.getTransaction().rollback();
        entityManager.getTransaction().begin();
        java.util.Collection data = query.getResultList();
        for (Object entity : data) {
            entityManager.refresh(entity);
        }
        list.clear();
        list.addAll(data);
    }//GEN-LAST:event_refreshButtonActionPerformed
    
    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        int[] selected = masterTable.getSelectedRows();
        List<contatospro.Telefones> toRemove = new ArrayList<contatospro.Telefones>(selected.length);
        for (int idx = 0; idx < selected.length; idx++) {
        contatospro.Telefones t = list.get(masterTable.convertRowIndexToModel(selected[idx]));
        String nome = ("Deseja Realmente Excluir o Telefone ?");  
        int opcao_escolhida = JOptionPane.showConfirmDialog(null,nome,"Excluir Telefone ",JOptionPane.YES_NO_OPTION);  
        if (opcao_escolhida==JOptionPane.YES_OPTION){  
        toRemove.add(t);
        entityManager.remove(t);
        }
        else  return;
        JOptionPane.showMessageDialog(null, "Telefone Excluído com Sucesso!!");
        }
        list.removeAll(toRemove);
    }//GEN-LAST:event_deleteButtonActionPerformed
    
    private void newButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newButtonActionPerformed
        contatospro.Telefones t = new contatospro.Telefones();
        entityManager.persist(t);
        list.add(t);
        int row = list.size() - 1;
        masterTable.setRowSelectionInterval(row, row);
        masterTable.scrollRectToVisible(masterTable.getCellRect(row, 0, true));
    }//GEN-LAST:event_newButtonActionPerformed
    
    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        try {
            entityManager.getTransaction().commit();
            entityManager.getTransaction().begin();
            JOptionPane.showMessageDialog(null, "Telefone Cadastrado com Sucesso!!");

        } catch (RollbackException rex) {
            rex.printStackTrace();
            entityManager.getTransaction().begin();
            List<contatospro.Telefones> merged = new ArrayList<contatospro.Telefones>(list.size());
            for (contatospro.Telefones t : list) {
                merged.add(entityManager.merge(t));
            }
            list.clear();
            list.addAll(merged);
            
            if ( (telefonejFormattedTextField.getText().isEmpty())
              ){
        JOptionPane.showMessageDialog(null,"O campo Telefone em Destaque azul é obrigatório", "Aviso!",  JOptionPane.WARNING_MESSAGE );
        return;
        }            
             
            if ( (pessoajComboBox.getSelectedItem()== null)              ){
        JOptionPane.showMessageDialog(null,"O campo Pessoa é obrigatório", "Aviso!",  JOptionPane.WARNING_MESSAGE );
        return;
        }
           
      }
    }//GEN-LAST:event_saveButtonActionPerformed

private void pessoajComboBox1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_pessoajComboBox1KeyReleased
// TODO add your handling code here:
     query = entityManager.createNamedQuery("Telefones.findByCodPessoa");
        query.setParameter("codPessoa", pessoajComboBox1.getSelectedItem());
        //Salvar o resultado em uma coleção
        java.util.Collection data = query.getResultList();
        pessoajComboBox1.setSelectedItem(null);
        //Limpar a lista atual com os dados da grade antigos
        list.clear();
        //Adicionar o que armazenado na variave data com o novo resultado
        list.addAll(data);
                 
}//GEN-LAST:event_pessoajComboBox1KeyReleased

private void buscarjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscarjButtonActionPerformed
// TODO add your handling code here:
    query = entityManager.createNamedQuery("Telefones.findByCodPessoa");
        query.setParameter("codPessoa", pessoajComboBox1.getSelectedItem());
        //Salvar o resultado em uma coleção
        java.util.Collection data = query.getResultList();
        pessoajComboBox1.setSelectedItem(null);
        //Limpar a lista atual com os dados da grade antigos
        list.clear();
        //Adicionar o que armazenado na variave data com o novo resultado
        list.addAll(data);
                 
}//GEN-LAST:event_buscarjButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buscarjButton;
    private javax.swing.JLabel codPessoaLabel;
    private javax.swing.JButton deleteButton;
    private javax.persistence.EntityManager entityManager;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JToolBar jToolBar1;
    private java.util.List<contatospro.Telefones> list;
    private javax.swing.JScrollPane masterScrollPane;
    private javax.swing.JTable masterTable;
    private javax.swing.JButton newButton;
    private javax.swing.JComboBox pessoajComboBox;
    private javax.swing.JComboBox pessoajComboBox1;
    private java.util.List<contatospro.Pessoas> pessoasList;
    private java.util.List<contatospro.Pessoas> pessoasList1;
    private java.util.List<contatospro.Pessoas> pessoasList2;
    private javax.persistence.Query pessoasQuery;
    private javax.persistence.Query pessoasQuery1;
    private javax.persistence.Query pessoasQuery2;
    private javax.persistence.Query query;
    private javax.swing.JButton refreshButton;
    private javax.swing.JButton saveButton;
    private javax.swing.JLabel telefoneLabel;
    private javax.swing.JFormattedTextField telefonejFormattedTextField;
    private contatospro.Telefones telefones1;
    private javax.swing.JLabel tipoTelefoneLabel;
    private javax.swing.JComboBox tipotelefonejComboBox;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
    public static void main(String[] args) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JFTelefones.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JFTelefones.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JFTelefones.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JFTelefones.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                JFrame frame = new JFrame();
                frame.setContentPane(new JFTelefones());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}
